﻿using Model.Public;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class JsonMisc
{
    public static List<int> JsonToListInt(string obj)
    {
        if (string.IsNullOrEmpty(obj))
        {
            return new List<int>();
        }
        List<object> lst = MiniJSON.Json.Deserialize(obj) as List<object>;
        List<int> res = new List<int>(lst.Count);
        for (int i = 0; i < lst.Count; i++)
        {
            res.Add(int.Parse(lst[i].ToString()));
        }
        return res;
    }

    public static string ListIntToJson(List<int> lst_int)
    {
        return MiniJSON.Json.Serialize(lst_int);
    }


    public static List<T> JsonToListObj<T>(string obj) where T : Model.Public.BaseEntity , new()
    {
        if (string.IsNullOrEmpty(obj))
        {
            return new List<T>();
        }
        List<object> lst = MiniJSON.Json.Deserialize(obj) as List<object>;
        List<T> res = new List<T>(lst.Count);
        for (int i = 0; i < lst.Count; i++)
        {
            T t = new T();

            t.DicChangeToModel(lst[i] as Dictionary<string, object>);

            res.Add(t);
        }
        return res;
    }

    public static List<T> JsonObjToListObj<T>(List<object> obj) where T : Model.Public.BaseEntity, new()
    {
        if (obj == null)
        {
            return new List<T>();
        }
        List<object> lst = obj;
        List<T> res = new List<T>(lst.Count);
        for (int i = 0; i < lst.Count; i++)
        {
            T t = new T();

            t.DicChangeToModel(lst[i] as Dictionary<string, object>);

            res.Add(t);
        }
        return res;
    }

    public static string ListObjToJson<T>(List<T> lst_obj) where T : Model.Public.BaseEntity, new()
    {
        List<object> json_lst = new List<object>(lst_obj.Count);
        for (int i = 0; i < lst_obj.Count; i++)
        {
            Dictionary<string, object> json = lst_obj[i].ModelChangeToDic();
            json_lst.Add(json);
        }
        return MiniJSON.Json.Serialize(json_lst);
    }

    public static List<object> ListObjToJsonObj<T>(List<T> lst_obj) where T : Model.Public.BaseEntity, new()
    {
        List<object> json_lst = new List<object>(lst_obj.Count);
        for (int i = 0; i < lst_obj.Count; i++)
        {
            Dictionary<string, object> json = lst_obj[i].ModelChangeToDic();
            json_lst.Add(json);
        }
        return json_lst;
    }
}
